package client;

import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Enumeration;
import java.util.Vector;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.DefaultListModel;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JScrollPane;

public class ListPanel extends JPanel implements MouseListener {
	private static final long serialVersionUID = 1L;
	DefaultListModel list;
	JScrollPane scroll;
	JList jlist;
	JButton but;
	JMenuItem del, save;
	JCheckBox box;
	ClientGUI parent;
	
	public ListPanel(ClientGUI parent) {
		super();
		this.parent = parent;
		list = new DefaultListModel();
		
		this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS));

		this.add(Box.createVerticalStrut(5));
		JLabel label = new JLabel("Liste des images");
		label.setAlignmentX(CENTER_ALIGNMENT);
		this.add(label);
		this.add(Box.createVerticalStrut(5));
		
		jlist = new JList(list);
		jlist.setAlignmentX(RIGHT_ALIGNMENT);
		save = new JMenuItem("Enregistrer localement ...");
		del = new JMenuItem("Supprimer");
		JPopupMenu menu = new JPopupMenu();
		menu.add(save);
		menu.addSeparator();
		menu.add(del);
		jlist.setComponentPopupMenu(menu);
		jlist.addMouseListener(this);
		scroll = new JScrollPane(jlist);
		this.add(scroll);
		this.add(Box.createVerticalStrut(5));
		
		box = new JCheckBox("Voir le XML");
		box.setAlignmentX(RIGHT_ALIGNMENT);
		this.add(box);
		this.add(Box.createVerticalStrut(5));
		
		but = new JButton("Creer");
		but.setAlignmentX(CENTER_ALIGNMENT);
		this.add(but);
		this.add(Box.createVerticalStrut(5));
	}
	
	public void updateList(String[] strList) {
		list.clear();

		for(String str : strList) {
			list.addElement(str);
		}
		
		/*jlist = new JList(list);
		jlist.setAlignmentX(RIGHT_ALIGNMENT);
		save = new JMenuItem("Enregistrer localement ...");
		del = new JMenuItem("Supprimer");
		JPopupMenu menu = new JPopupMenu();
		menu.add(save);
		menu.addSeparator();
		menu.add(del);
		jlist.setComponentPopupMenu(menu);
		jlist.addMouseListener(this);
		jlist.addListSelectionListener(parent);*/
		jlist.updateUI();
		this.updateUI();
		
		/*for(Enumeration e = ((DefaultListModel)(jlist.getModel())).elements(); e.hasMoreElements();) {
			System.out.println(e.nextElement());
		}*/
	}

	@Override
	public void mouseClicked(MouseEvent arg0) {
	}

	@Override
	public void mouseEntered(MouseEvent arg0) {
	}

	@Override
	public void mouseExited(MouseEvent arg0) {
	}

	@Override
	public void mousePressed(MouseEvent arg0) {
	}

	@Override
	public void mouseReleased(MouseEvent arg0) {
		if(arg0.getButton() == MouseEvent.BUTTON3) {
			jlist.setSelectedIndex(jlist.locationToIndex(arg0.getPoint()));
		}
	}
}
